#ifndef LAYERMANAGER_H
#define LAYERMANAGER_H

#include <QObject>
#include <QMap>
#include <QListIterator>
#include "layer.h"
#include "mapadapter.h"
#include "mapcontrol.h"

class Layer;
class MapAdapter;
class MapControl;

class LayerManager;

class LayerManager : public QObject
{
    Q_OBJECT

public:
    LayerManager(MapControl*, QSize);
    ~LayerManager();

    QPointF currentCoordinate() const;

    QPixmap getImage() const;

    Layer* layer(const QString&) const;

    Layer* layer() const;

    QList<QString> layers() const;

    void setView(const QPointF& coordinate);

    void setView(const QList<QPointF> coordinates);

    void setViewAndZoomIn (const QList<QPointF> coordinates);

    void zoomIn();

    void zoomOut();

    void setZoom(int zoomlevel);

    QRectF getViewport() const;

    void scrollView(const QPoint& offset);

    void mouseEvent(const QMouseEvent* evnt);

    QPoint getMapmiddle_px() const;

    void forceRedraw();
    void removeZoomImage();

    void addLayer(Layer* layer);

    int currentZoom() const;

    void drawGeoms(QPainter* painter);
    void drawImage(QPainter* painter);

private:
    LayerManager& operator=(const LayerManager& rhs);
    LayerManager(const LayerManager& old);
    void newOffscreenImage(bool clearImage=true, bool showZoomImage=true);
    inline bool checkOffscreen() const;
    inline bool containsAll(QList<QPointF> coordinates) const;
    inline void moveWidgets();
    inline void setMiddle(QList<QPointF> coordinates);

    MapControl* mapcontrol;
    QPoint screenmiddle; // middle of the screen
    QPoint scroll; // scrollvalue of the offscreen image
    QPoint zoomImageScroll; // scrollvalue of the zoom image

    QSize size; // widget size
    QSize offSize; // size of the offscreen image

    QPixmap composedOffscreenImage;
    QPixmap composedOffscreenImage2;
    QPixmap zoomImage;

    QList<Layer*> m_layers;

    QPoint mapmiddle_px; // projection-display coordinates
    QPointF mapmiddle;   // world coordinate

    QPoint whilenewscroll;

public slots:
    void updateRequest(QRectF rect);
    void updateRequest();
    void resize(QSize newSize);
};
#endif
